<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <!-- 编辑按钮 -->
      <van-button class="edit-btn" size="mini" round
      type="danger" plain @click="isEdit = !isEdit">
      {{ isEdit ? '完成' : '编辑' }}</van-button>
    </van-cell>
    <!-- 我的的频道 -->
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
      class="grid-item"
      v-for="(channel, index) in userChannels"
      :key="index"
      @click="onAllChannelsClick(channel, index)">
        <!-- 删除图标 -->
        <van-icon
        v-show="isEdit && !fiexdChannels.includes(channel.id)"
        slot="icon" name="clear"></van-icon>

        <!-- 选中高亮 -->
        <span class="text" :class="{ active: index === activeIndex }" slot="text">{{ channel.name }}</span>
      </van-grid-item>
    </van-grid>

    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <!-- 推荐的频道 -->
    <van-grid class="recomment-grid" :gutter="10">
      <van-grid-item
      icon="plus"
      class="grid-item"
      v-for="channel in recommendChannele"
      :key="channel.id"
      :text="channel.name"
      @click="onAddChannel(channel)"/>
    </van-grid>
  </div>
</template>

<script>

// 获取 json 数据
import channelsList from '@/json/home/channels.json'
// 导入接口
import { mapState } from 'vuex'
// 处理未登录模块，导入封装的本地存储模块
import { setItem } from '@/utils/storage.js'
export default {
  name: 'ChannelEdit',
  props: {
    userChannels: {
      type: Array,
      required: true
    },
    activeIndex: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      // 所有频道
      allChannels: [],
      // 控制编辑状态
      isEdit: false,
      // 不需要编辑、删除的频道
      fiexdChannels: [0]
    }
  },
  computed: {
    // 区分频道
    recommendChannele () {
      // 数组的 filter 方法：遍历数组，把符合条件的元素存储到新数组中
      return this.allChannels.filter(channel => {
        // 数组的 find 方法：遍历数组，把符合条件的第一个元素返回,做条件
        return !this.userChannels.find(allChannels => {
          return allChannels.id === channel.id
        })
      })
    },
    // 展开
    ...mapState(['user'])
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    // 展示频道数据
    async loadAllChannels () {
      try {
        this.allChannels = channelsList
      } catch (error) {
        this.$toast('获取所有频道数据失败')
      }
    },
    // 添加频道
    async onAddChannel (channel) {
      this.userChannels.push(channel)

      // 把数据存储到本地
      setItem('TOUTIAO_CHANNELS', this.userChannels)
    },
    // 切换频道,判断事件
    onAllChannelsClick (channel, index) {
      if (this.isEdit) {
        // 如果是固定频道，则不删除
        if (this.fiexdChannels.includes(channel.id)) return

        // 编辑状态，执行删除频道
        // 参数1:要删除的元素的索引
        // 参数2:删除的个数，如果不指定，则从参数1开始一直删除到最后
        this.userChannels.splice(index, 1)

        // 让激活频道索引 - 1
        if (index <= this.activeIndex) this.$emit('update-active', this.activeIndex - 1, true)

        // 处理持久化操作
        this.deleteChannel(channel)
      } else {
        // 非编辑状态，指向切换频道
        this.$emit('update-active', index, false)
      }
    },
    // 删除频道
    async deleteChannel (channel) {
      try {
        // 将数据更新到本地
        setItem('TOUTIAO_CHANNELS', this.userChannels)
      } catch (error) {
        this.$toast('操作失败，请稍后重试')
      }
    }
  }
}

</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;

  .title-text {
    font-size: 32px;
    color: #333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;

      .van-grid-item__text, .text {
        font-size: 22px;
        color: #222;
        margin-top: 4px !important;
      }
       .active {
        color: red;
      }

      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recomment-grid {
    .grid-item {
      width: 160px;
      height: 86px;
      .van-grid-item__content {
        flex-direction: row;

        .van-icon-plus {
          font-size: 22px;
          line-height: 22px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
